package com.example.rbacmanage.service.app.impl;

import com.example.rbacmanage.annotation.PermissionRange;
import com.example.rbacmanage.entity.app.AppMenu;
import com.example.rbacmanage.entity.app.MenuOrder;
import com.example.rbacmanage.mapper.app.AppMenuMapper;
import com.example.rbacmanage.mapper.app.MenuOrderMapper;
import com.example.rbacmanage.service.app.AppMenuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

@Service
public class AppMenuServiceImpl implements AppMenuService {
    @Autowired
    private AppMenuMapper appMenuMapper;

    @Autowired
    private MenuOrderMapper menuOrderMapper;

    @Override
    public void save(AppMenu appMenu) {
        appMenu.setCreateTime(new Date());
        appMenuMapper.insert(appMenu);
    }

    @Override
    public void remove(Long[] ids) {
        for (Long id : ids) {
            appMenuMapper.del(id);
        }
    }

    @Override
    public void update(AppMenu appMenu) {
        appMenuMapper.update(appMenu);
    }

    @PermissionRange(shopAlias = "s")
    @Override
    public List<AppMenu> getAll(AppMenu appMenu) {
        List<AppMenu> list = appMenuMapper.list(appMenu);
        //查询所有菜单，取出前三名销量的菜单为火热
        List<AppMenu> all = appMenuMapper.list(new AppMenu());
        List<Long> menuIds = all.stream().map(AppMenu::getId).collect(Collectors.toUnmodifiableList());
        if (!menuIds.isEmpty()){
            MenuOrder menuOrder = new MenuOrder();
            menuOrder.setMenuIds(menuIds);
            List<MenuOrder> menuOrders = menuOrderMapper.list(menuOrder);
            all.forEach(menu->{
                //获取已完成订单数
                AtomicReference<Integer> orderNum = new AtomicReference<>(0);
                menuOrders.forEach(order ->{
                    if (order.getOrderStatus().equals("1") && menu.getId().equals(order.getMenuId())){
                        orderNum.getAndSet(orderNum.get() + 1);
                    }
                });
                menu.setSalesVolume(orderNum.get());
            });
        }
        Collections.sort(all, new Comparator<AppMenu>() {
            @Override
            public int compare(AppMenu p1, AppMenu p2) {
                return Integer.compare(p2.getSalesVolume(), p1.getSalesVolume()); // 降序排序
            }
        });
        List<Long> fires = new ArrayList<>();
        for (int i=0;i<all.size();i++){
            if (i < 3 && all.get(i).getSalesVolume() > 0){
                fires.add(all.get(i).getId());
            }
        }
        for (AppMenu appmenu : list) {
            for (AppMenu menu : all) {
                if (appmenu.getId().equals(menu.getId())){
                    appmenu.setSalesVolume(menu.getSalesVolume());
                    break;
                }
            }
            if (fires.contains(appmenu.getId())){
                appmenu.setFire(true);
            }else{
                appmenu.setFire(false);
            }
        }
        return list;
    }

    @PermissionRange(shopAlias = "s")
    @Override
    public PageInfo<AppMenu> getPage(AppMenu appMenu) {
        PageHelper.startPage(appMenu.getPage(),appMenu.getLimit());
        return new PageInfo<>(getAll(appMenu));
    }

    @Override
    public AppMenu getById(Long id) {
        return appMenuMapper.queryOneById(id);
    }

    @Override
    public Integer getCount(AppMenu appMenu) {
        return appMenuMapper.count(appMenu);
    }

    @Override
    public PageInfo<AppMenu> page(AppMenu appMenu) {
        PageHelper.startPage(appMenu.getPage(),appMenu.getLimit());
        return new PageInfo<>(getAll(appMenu));
    }
}
